<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Post\BatchRestore;
use App\Admin\Actions\Post\Restore;
use App\Models\Good;
use App\Models\GoodsType;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class GoodController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Good(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('goods_name');
            $grid->column('goods_price');
            $grid->column('goods_type','商品分类')
                ->display(function ($e){
                    $goods_type=(new GoodsType())->where('id',$e)->pluck('type_name')->first();
                    return $goods_type;
                });
            $grid->column('goods_amount');
            $grid->column('goods_image')->image('',100,100);
            $grid->column('goods_item')->display(function ($pictures) {
                return json_decode($pictures, true);
            })->image('http://xxx.com', 50, 50);
            $grid->column('goods_describe');
            $grid->column('goods_status')->display(function ($e) {
                return $e == 1 ? true : false;
            })->switch('', true);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

//            //分类筛选
//            $grid->selector(function (Grid\Tools\Selector $selector) {
//                dd($selector);
//            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                // 范围过滤器，调用模型的`onlyTrashed`方法，查询出被软删除的数据。
                $filter->scope('trashed', '回收站')->orderBy('deleted_at','desc')->onlyTrashed();
            });
            //行恢复
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if (request('_scope_') == 'trashed') {
                    $actions->append(new Restore(Good::class));
                }
            });
            //批量恢复
            $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
                if (request('_scope_') == 'trashed') {
                    $batch->add(new BatchRestore(Good::class));
                }
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Good(), function (Show $show) {
            $show->field('id');
            $show->field('goods_name');
            $show->field('goods_price');
            $show->field('goods_type');
            $show->field('goods_amount');
            $show->field('goods_image');
            $show->field('goods_item');
            $show->field('goods_describe');
            $show->field('goods_status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Good(), function (Form $form) {
            $form->display('id');
            $form->text('goods_name');
            $form->text('goods_price');
//            $form->text('goods_type');
            $form->text('goods_amount');
            $form->image('goods_image','商品封面图')->disk('qiniu')->uniqueName()->saveFullUrl()->autoUpload();
            $form->multipleFile('goods_item')->saving(function ($paths) {
                return json_encode($paths);
            })->disk('qiniu')->uniqueName()->saveFullUrl()->autoUpload();
            $form->editor('goods_describe');
            $form->radio('goods_status')->required()->options(['1' => '上架', '0' => '下架'])->default('1');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
